package com.example.springbootbabyu.tools;

import com.example.springbootbabyu.entity.*;
import com.example.springbootbabyu.service.CategoryService;
import com.example.springbootbabyu.service.GoodsService;
import com.example.springbootbabyu.service.HomeService;
import com.example.springbootbabyu.service.MemberService;
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 功能
 * 作者：梦天
 * 日期：2025/6/18 15:26
 */
import com.fasterxml.jackson.core.JsonProcessingException;
import java.util.List;

@Component
public class AgentTools {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private HomeService homeService;

    @Autowired
    private MemberService memberService;
    @Autowired
    private ObjectMapper objectMapper;
    // 根据商品名称查询商品列表
    @Tool(name = "getGoodsByName", value = "根据商品名称查询商品列表")
    @Schema(description = "根据商品名称查询商品列表")
    public String getGoodsByNameTool(@P("name") String name) {
        List<GoodsDetail> goods = goodsService.getGoodsByName(name);
        GoodsDetail detail = new GoodsDetail();
        for (GoodsDetail good : goods){
            detail = goodsService.getGoodsDetail(good.getId());
        }
        if (detail == null) {
            if (goods == null || goods.isEmpty()) {
                return "没有找到名称为「" + name + "」的商品";
            }
        }
        return toJson(detail);
    }
    // 根据 SKU ID 查询库存信息
    @Tool(name = "getStockById", value = "根据SKU的ID查询库存信息")
    @Schema(description = "查询商品库存详情")
    public String getStockByIdTool(@P("skuId") String skuId) {
        Sku sku = goodsService.getStockById(skuId);
        if (sku == null) {
            return "没有找到 ID 为「" + skuId + "」的库存信息";
        }
        return toJson(sku);
    }
    // 获取商品详情
    @Tool(name = "getGoodsDetail", value = "根据商品ID查询商品详情")
    @Schema(description = "获取商品详情信息")
    public String getGoodsDetailTool(@P("id") String id) {
        GoodsDetail detail = goodsService.getGoodsDetail(id);
        if (detail == null) {
            return "没有找到 ID 为「" + id + "」的商品详情";
        }
        return toJson(detail);
    }
    // 获取热销商品列表
    @Tool(name = "getHotGoods", value = "根据 ID、类型和数量获取热销商品")
    @Schema(description = "获取热销商品列表")
    public String getHotGoodsTool(@P("id") String id, @P("type") String type, @P("limit") Integer limit) {
        List<HotGoodsVO> hotGoods = goodsService.getHotGoods(id, type, limit);
        if (hotGoods == null || hotGoods.isEmpty()) {
            return "没有找到符合条件的热销商品";
        }
        return toJson(hotGoods);
    }
    // 获取相关商品列表
    @Tool(name = "getRelevantGoods", value = "根据数量获取相关商品")
    @Schema(description = "获取相关商品列表")
    public String getRelevantGoodsTool(@P("limit") Integer limit) {
        List<Goods> relevantGoods = goodsService.getRelevantGoods(limit);
        if (relevantGoods == null || relevantGoods.isEmpty()) {
            return "没有找到相关商品";
        }
        return toJson(relevantGoods);
    }

    // 获取缓存中的商品详情
    @Tool(name = "getGoodsDetailCache", value = "根据商品 ID 获取缓存中的商品详情")
    @Schema(description = "从缓存获取商品详情信息")
    public String getGoodsDetailCacheTool(@P("id") String id) {
        GoodsDetail cachedDetail = goodsService.getGoodsDetailCache(id);
        if (cachedDetail == null) {
            return "缓存中没有找到 ID 为「" + id + "」的商品详情";
        }
        return toJson(cachedDetail);
    }
    @Tool(name = "getSubCategories", value = "获取子分类列表")
    @Schema(description = "根据父分类 ID 获取子分类列表")
    public String getSubCategoriesTool(@P("parentId") String parentId) {
        CategoryResult result = categoryService.getSubCategories(parentId);
        if (result == null) {
            return "没有找到父ID为「" + parentId + "」的子分类信息";
        }
        return toJson(result);
    }

    @Tool(name = "getCategoryFilter", value = "获取分类筛选条件")
    @Schema(description = "根据分类 ID 获取筛选条件")
    public String getCategoryFilterTool(@P("categoryId") String categoryId) {
        CategorySubFilter filter = categoryService.getCategoryFilter(categoryId);
        if (filter == null) {
            return "没有找到分类 ID 为「" + categoryId + "」的筛选条件";
        }
        return toJson(filter);
    }

    @Tool(name = "getSubCategoryGoods", value = "获取子分类商品列表")
    @Schema(description = "根据子分类查询商品列表")
    public String getSubCategoryGoodsTool(@P("queryDTO") SubCategoryQueryDTO queryDTO) {
        CategoryGoodsResult goodsResult = categoryService.getSubCategoryGoods(queryDTO);
        if (goodsResult == null) {
            return "没有找到符合条件的商品";
        }
        return toJson(goodsResult);
    }
    @Tool(name = "getHomeCategoryHead", value = "获取首页分类头部信息")
    @Schema(description = "获取首页分类头部信息")
    public String getHomeCategoryHeadTool() {
        List<Category> categories = homeService.getHomeCategoryHead();
        if (categories == null || categories.isEmpty()) {
            return "没有找到首页分类头部信息";
        }
        return toJson(categories);
    }

    @Tool(name = "getHomeHot", value = "获取首页热销商品")
    @Schema(description = "获取首页热销商品列表")
    public String getHomeHotTool() {
        List<Hot> hots = homeService.getHomeHot();
        if (hots == null || hots.isEmpty()) {
            return "没有找到首页热销商品";
        }
        return toJson(hots);
    }

    @Tool(name = "getHomeBrand", value = "获取首页品牌推荐")
    @Schema(description = "获取首页品牌推荐列表")
    public String getHomeBrandTool() {
        List<Brand> brands = homeService.getHomeBrand();
        if (brands == null || brands.isEmpty()) {
            return "没有找到首页品牌推荐";
        }
        return toJson(brands);
    }
    @Tool(name = "getUserCart", value = "获取用户购物车")
    @Schema(description = "根据用户 ID 获取购物车列表")
    public String getUserCartTool(@P("userId") String userId) {
        List<CartVo> cart = memberService.getCard(userId);
        if (cart == null || cart.isEmpty()) {
            return "没有找到用户 ID 为「" + userId + "」的购物车信息";
        }
        return toJson(cart);
    }

    @Tool(name = "getPreOrder", value = "获取订单预览信息")
    @Schema(description = "根据用户 ID 获取订单预览信息")
    public String getPreOrderTool(@P("userId") String userId) {
        PreOrderVo preOrder = memberService.getPreOrder(userId);
        if (preOrder == null) {
            return "没有找到用户 ID 为「" + userId + "」的订单预览信息";
        }
        return toJson(preOrder);
    }

    @Tool(name = "mergeCart", value = "合并购物车")
    @Schema(description = "将商品加入购物车并合并")
    public String mergeCartTool(
            @P("userId") String userId,
            @P("cartItems") List<CartItemDTO> cartItems
    ) {
        String result = memberService.mergeCart(userId, cartItems);
        return toJson(result);
    }
    // 封装 JSON 序列化方法
    private String toJson(Object obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            return "序列化失败: " + e.getMessage();
        }
    }
}
